package com.yan.test.work.testadapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.yan.test.work.testadapter.databinding.LayoutItemBinding

/**
 * Create By : 'YWY'
 * On : 2024/4/14
 */
class MainAdapter(val gridLayoutManager: GridLayoutManager) :RecyclerView.Adapter<MainAdapter.VH>() {

    inner class VH(val mbinding: LayoutItemBinding) :RecyclerView.ViewHolder(mbinding.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH =
        VH(LayoutItemBinding.inflate(LayoutInflater.from(parent.context),parent , false))

    override fun getItemCount(): Int  = 10

    override fun onBindViewHolder(holder: VH, position: Int) {
        //获取内容layout的parms
        val parm = holder.itemView.getLayoutParams();
        //通过获取gridlayoutmanager的个数于宽度动态给parm赋予高度
        parm.height = gridLayoutManager.getWidth()/ gridLayoutManager.getSpanCount()
        - 2 * holder.itemView.getPaddingLeft() - 2*(parm as ViewGroup.MarginLayoutParams).leftMargin



        holder.mbinding.itemTv.text = "${position}"
    }


}